
const lodash = require("lodash")
const loginFns = require("../common/login")
import { pageFns } from '../utils/pageHelper'
import { SourceDao } from '@/dao/sourceDao'
import { ArticleStatus } from '@/entities/Constant'
const AutoScrollDown = require('puppeteer-autoscroll-down')

const SELECTORS = { "ITEMLINK": ".swiper-slide-active .goods-card:has(.price-container)", "NEXTPAGE": "" }

const LOGINS = {}

async function goSpider(page, task, source, rule) {
    let selectors = JSON.parse(rule.codeRule)
    if (lodash.isEmpty(selectors)) {
        return { err: "there is not selectors of row:" + JSON.stringify(source) }
    }
    try {
        //是否需要登录
        if (task.accountId) {
            let res = await loginFns.checkLogin(page, task, LOGINS)
            if (res.err) {
                console.log("goSpider.checkLogin", res)
                return res
            }
        }
        let res = await pageFns.runResetTimout(source.link, page, async (pageUrl, page) => {
            await page.goto(pageUrl, { waitUntil: 'networkidle0' })
            return { err: null }
        }, { timeout: 60000 })
        if (res.err) {
            throw new Error(res.err)
        }
        await AutoScrollDown.scrollPageToBottom(page, {
            size: 300, delay: 3 * 1000, stepsLimit: 100
        })
        await fetchItems(source, page, 1, source.maxRow, selectors)
        return { data: source.count }
    } catch (err) {
        return { err: err }
    }
}

async function fetchItems(row, page, pageNum, rowMax, selectors) {
    // 获取item的href
    let attributes = await pageFns.attributeNames(page, selectors.ITEMLINK, "data-eaglet")
    for (let attribute of attributes) {
        let attributeObj = JSON.parse(attribute)
        let goodId = attributeObj.mallGoodsTarget.value.goodsId
        let href = `https://www.xiaohongshu.com/goods-detail/${goodId}?openPage=yes`
        // 存入source中
        let whereDict = { link: href }
        let sourceExist = await SourceDao.countWhere(whereDict)
        if (sourceExist <= 0) {
            let obj = SourceDao.toDetail(row, href)
            await SourceDao.save(obj)
        }
    }
    //点击上一页
    if (rowMax > pageNum) {
        await pageFns.waitAndClick(page, selectors.NEXTPAGE)
        pageNum = pageNum + 1
        await fetchItems(row, page, pageNum, rowMax, selectors)
    } else {
        row.status = ArticleStatus.Gather
        row.count = await SourceDao.countWhere({ sourceId: row.id });
        await SourceDao.save(row)
    }
}

export { goSpider }